package org.nowcoder.leetcode.BTree;

/**
 * Title  : 700. Search in a Binary Search Tree
 * Source : https://leetcode.cn/problems/search-in-a-binary-search-tree/description/
 * Author : XrazYang
 * Date   : 2024-04-18
 */
public class LeetCode_700 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode searchBST(TreeNode root, int val) {
        if (root != null) {
            if (root.val == val) {
                return root;
            } else if (root.val < val) {
                return searchBST(root.right, val);
            } else {
                return searchBST(root.left, val);
            }
        }
        return null;
    }
}
